﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using BookStore.Solution.Entities;
using BookStore.Solution.BLL;
using BookStore.Solution.Utilities;
using System.IO;
using BookStore.Solution.Configurations;

namespace BookStore.Solution.Presentation
{
    public partial class FrmStationery : DevComponents.DotNetBar.OfficeForm
    {
        private bool addNew = true;
        Product product;
        string result = "";
        private bool viewDetail = false;
        public FrmStationery()
        {
            InitializeComponent();
        }

        public FrmStationery(string productID, bool detail)
        {
            InitializeComponent();
            addNew = false;
            if (detail)
            {
                viewDetail = true;
                txtProductID.Enabled = false;
                this.Text = "Xem chi tiết văn phòng phẩm: " + productID;
                btnSubmit.Text = "Đóng cửa sổ";
                btnSubmit.Width = 90;
                btnSubmit.Location = new Point(((int)this.Width/2 - 45),btnSubmit.Location.Y);
                btnReset.Visible = false;           
            }
            else
            {
                this.Text = "Cập nhật thông tin văn phòng phẩm: " + productID;
                txtProductID.Enabled = false;
            }
            StationeryManager stationeryManager = new StationeryManager();
            product = new Product();
            product = stationeryManager.GetbyID(productID);
            txtProductID.Text = product.ProductID;
            txtProductName.Text = product.ProductName;
            cbbCategory.SelectedValue = product.CategoryID;
            txtUnit.Text = product.Unit;
            txtUnitPrice.Text = product.UnitPrice.ToString();
            txtQuantity.Text = product.Quatity.ToString();
            txtDescription.Text = product.Decription;
            if (product.Picture != "")
            {
                txtPath.Text = product.Picture;
                pbPhoto.ImageLocation = product.Picture;
            }
            if (product.Status.Equals("Còn hàng"))
            {
                rdAvailable.Checked = true;
            }
            else if (product.Status.Equals("Hết hàng"))
            {
                rdUnavailable.Checked = true;
            }
            else
            {
                rdNoBusiness.Checked = true;
            }
        }
        private void LoadCategory()
        {
            CategoryManager category = new CategoryManager();
            cbbCategory.DataSource = category.GetAll("Văn phòng phẩm");
            cbbCategory.DisplayMember = "CategoryName";
            cbbCategory.ValueMember = "CategoryID";
        }

        private void FrmStationery_Load(object sender, EventArgs e)
        {
            LoadCategory();
        }
        private void btnBrowse_Click(object sender, EventArgs e)
        {
            try
            {
                if (Ofd.ShowDialog() == DialogResult.OK)
                {
                    pbPhoto.Image = Image.FromFile(Ofd.FileName);
                    txtPath.Text = Ofd.FileName.ToString();
                }
            }
            catch
            {
                pbPhoto.Image = null;
            }
        }
        private void CopyImage()
        {
            string new_dir = Configs.ImageFolderPath;
            string fileName = System.IO.Path.GetFileName(txtPath.Text);
            string fName = System.IO.Path.Combine(new_dir, fileName);
            if (!File.Exists(fName))
                System.IO.File.Copy(txtPath.Text, fName);
            txtPath.Text = fName;
        }
        private void btnDelPicture_Click(object sender, EventArgs e)
        {
            txtPath.Text = "";
            pbPhoto.ResetText();
            pbPhoto.Image = null;
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            if (viewDetail)
            {
                this.Close();
            }
            else
            {

                if (checkValidation())
                {
                    product = new Product();
                    StationeryManager stationeryManager = new StationeryManager();
                    if (txtPath.Text != "")
                        CopyImage();
                    product.ProductID = txtProductID.Text;
                    product.ProductName = txtProductName.Text;
                    try
                    {
                        product.CategoryID = cbbCategory.SelectedValue.ToString();

                    }
                    catch
                    {
                        MessageBox.Show("Bạn chưa chọn danh mục!!!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }
                    product.Decription = txtDescription.Text;
                    product.Unit = txtUnit.Text;
                    product.UnitPrice = long.Parse(txtUnitPrice.Text);
                    product.Quatity = int.Parse(txtQuantity.Text);
                    product.Picture = txtPath.Text;
                    if (rdAvailable.Checked)
                    {
                        product.Status = "Còn hàng";
                    }
                    else if (rdUnavailable.Checked)
                    {
                        product.Status = "Hết hàng";
                    }
                    else
                    {
                        product.Status = "Không kinh doanh";
                    }
                    product.Type = "VPP";

                    if (addNew)
                    {
                        result = stationeryManager.Insert(product);
                    }
                    else
                    {
                        result = stationeryManager.Update(product);
                    }
                    MessageBox.Show(result, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.Close();
                }
                else
                {
                    MessageBox.Show("Thêm không thành công, vui lòng kiểm tra lại các thông tin bị thiếu hoặc không đúng định dạng", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
        }
        private bool checkValidation()
        {
            if (txtProductID.Text == "")
            {
                lblProductID.ForeColor = Color.Red;
                return false;
            }
            if (txtProductName.Text == "")
            {
                lblProductName.ForeColor = Color.Red;
                return false;
            }
            int quan = DataUtil.ToInt(txtQuantity.Text, -1);
            long price = DataUtil.ToLong(txtUnitPrice.Text, 0);
            if (quan < 0)
            {
                this.lblQuantity.ForeColor = Color.Red;
                return false;
            }
            if (price == 0 || txtUnitPrice.Text == "")
            {
                this.lblUnitPrice.ForeColor = Color.Red;
                return false;
            }
            return true;
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            if (!addNew)
                txtProductID.ResetText();
            txtProductName.ResetText();
            txtQuantity.ResetText();
            txtDescription.ResetText();
            txtPath.ResetText();
            txtUnitPrice.ResetText();
            txtUnit.ResetText();
        }
    }
}